<?php

namespace App\Admin\Controllers;

use App\Models\Campus;

use App\Models\University;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;


class CampusController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Request $request)
    {
        $university_id=$request->input('university_id');
        return Admin::content(function (Content $content) use ($university_id){

            $content->header('校区');
            $content->description('校区列表');

            $content->body($this->grid($university_id));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($university_id)
    {
        return Admin::grid(Campus::class, function (Grid $grid) use($university_id){
            if($university_id){
                $grid->model()->where('university_id', $university_id);
            }
            $grid->id('ID')->sortable();
            $grid->name('校区名称');
            $grid->university('大学')->display(function($data){
                return $data['name'];
            });
            $grid->address('地址');
            $grid->desc('描述');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Campus::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name','校区名称');
            $university=University::all();
            foreach($university as $v){
                $options[$v->id]=$v->name;
            }
            $form->select('university_id','所属大学')->options($options);
            $form->text('desc','描述');
            $form->text('address','地址');
        });
    }
}
